package com.ys.otms.utils;

import net.sf.json.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class HttpUtil {

    private static Logger logger = LoggerFactory.getLogger(HttpUtil.class);

    public static JSONObject get(String url) {
        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
        CloseableHttpClient httpClient = httpClientBuilder.build();
        HttpGet httpGet = new HttpGet(url);
        JSONObject jsonObject;
        HttpResponse httpResponse;
        try {
            httpResponse = httpClient.execute(httpGet);
//            logger.info("HttpUtil get getStatusLine:{}", httpResponse.getStatusLine());
            jsonObject = getRetJSONObject(httpResponse);
        }
        catch (Exception e) {
            logger.error("HttpUtil get Exception e:{}", e);
            jsonObject = null;
        }
//        logger.info("HttpUtil get jsonObject:{}", jsonObject);
        return jsonObject;
    }

    public static JSONObject post(String url, Map<String, String> params) {

        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
        CloseableHttpClient httpClient = httpClientBuilder.build();
        HttpPost httpPost = new HttpPost(url);
        JSONObject jsonObject;
        HttpResponse httpResponse;
        try {
            if (null != params) {
                List<NameValuePair> nameValuePairList = new ArrayList<>();
                for (String key : params.keySet()) {
                    BasicNameValuePair basicNameValuePair = new BasicNameValuePair(key, params.get(key));
                    nameValuePairList.add(basicNameValuePair);
                }
                httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairList, "utf-8"));
            }
            httpResponse = httpClient.execute(httpPost);
//            logger.info("HttpUtil post getStatusLine:{}", httpResponse.getStatusLine());
            jsonObject = getRetJSONObject(httpResponse);
        }
        catch (Exception e) {
            logger.error("HttpUtil post Exception e:{}", e);
            jsonObject = null;
        }
//        logger.info("HttpUtil post jsonObject:{}", jsonObject);
        return jsonObject;
    }

    private static JSONObject getRetJSONObject(HttpResponse httpResponse) {

        try {
            HttpEntity httpEntity = httpResponse.getEntity();
            if (null != httpEntity) {
                String result = EntityUtils.toString(httpEntity);
                return JSONObject.fromObject(result);
            }
        } catch (IOException e) {
            logger.error("HttpUtil getRetJSONObject Exception e:{}", e);
        }
        return null;
    }

}
